<template>
  <div>
    <el-dialog
      title="修改往来单位"
      :visible.sync="dialogVisible"
      lock-scroll
      width="60%"
      @close="dialogVisibleClose"
    >
      <div class="dialog_div_new">
        <div class="dialog_middle">
          <div class="input_line">
            <div>
              <p>单位名称</p>
              <p style="color: red">*</p>
              <input
                type="text"
                ref="nameRef"
                @focusout="getNameToPinying"
                v-model="newData.companyName"
              />
            </div>
            <div>
              <p>联系人</p>
              <input type="text" v-model="newData.companyUserName" />
            </div>
          </div>
          <div class="input_line">
            <div>
              <p>单位性质</p>
              <el-select v-model="newData.xghCompanyNature" @change="onCompanyNature">
                <el-option
                  v-for="item in CompanyNatureOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
            <div>
              <p>联系电话</p>
              <input type="text" v-model="newData.companyPhone" />
            </div>
          </div>
          <div class="input_line">
            <div>
              <p>拼音编码</p>
              <input type="text" ref="nameRef" v-model="newData.companyPinyinCode" />
            </div>
            <div>
              <p>手机</p>
              <input type="text" v-model="newData.companyLandline" />
            </div>
          </div>
          <div class="input_line">
            <div class="line_dialog">
              <p>单位类别</p>
              <p class="dialog_input">
                <input @click="onShowTree" type="text" :value="newData.companyTypeName" />
                <i @click="onShowTree" class="el-icon-more"></i>
              </p>
            </div>
            <div>
              <p>所属业务</p>
              <input type="text" v-model="newData.business" />
            </div>
          </div>
          <div class="input_line">
            <div class="line_areaAddress">
              <p>行政区划</p>
              <div>
                <input disabled class="address_china" type="text" value="中国" />
                <el-cascader
                  size="large"
                  placeholder="请选择省/市/县区"
                  :options="ssqOptions"
                  v-model="selectedOptions"
                  @change="addressChange"
                ></el-cascader>
              </div>
            </div>
            <div class="line_detailedAddress">
              <p>详细地址</p>
              <input type="text" v-model="newData.address" />
              <el-button type="primary">识别</el-button>
            </div>
          </div>
          <div class="input_line">
            <div>
              <p>预设售价</p>
              <el-select v-model="newData.setPrice">
                <el-option
                  v-for="item in PresetPriceOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
            <div>
              <p>预设折扣</p>
              <el-select v-model="newData.setDiscountPrice">
                <el-option
                  v-for="item in PresetDiscountOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </div>
          <div class="input_line">
            <div>
              <p>单位编码</p>
              <p style="color: red">*</p>
              <input type="text" ref="nameRef" v-model="newData.companyNumber" />
            </div>
            <div></div>
          </div>
          <div class="input_switch">
            <div>
              <p>开票信息</p>
              <el-switch
                v-model="newData.companyInvoiceStatus"
                active-color="#1A93FA"
                inactive-color="#D3D2D2"
              ></el-switch>
            </div>
          </div>
          <div v-show="newData.companyInvoiceStatus" class="input_line">
            <div>
              <p>开户银行</p>
              <input type="text" v-model="newData.depositBank" />
            </div>
            <div>
              <p>银行账户</p>
              <input type="text" v-model="newData.bankName" />
            </div>
          </div>
          <div v-show="newData.companyInvoiceStatus" class="input_line">
            <div>
              <p>银行账号</p>
              <input type="text" ref="nameRef" v-model="newData.bakCard" />
            </div>
            <div>
              <p>企业税号</p>
              <input type="text" v-model="newData.taxNumber" />
            </div>
          </div>
          <div v-show="newData.companyInvoiceStatus" class="input_line">
            <div>
              <p>开票地址</p>
              <input type="text" v-model="newData.invoiceAddress" />
            </div>
            <div>
              <p>电话</p>
              <input type="text" v-model="newData.bankPhone" />
            </div>
          </div>
          <div class="input_switch p_width">
            <div>
              <p>应收应付</p>
              <el-switch
                v-model="newData.companyRAndPStatus"
                active-color="#1A93FA"
                inactive-color="#D3D2D2"
              ></el-switch>
            </div>
          </div>
          <div v-show="newData.companyRAndPStatus" class="input_line p_width">
            <div>
              <p>期初应收款</p>
              <input type="text" :value="newData.beginReceivable" />
            </div>
            <div>
              <p>期初应付款</p>
              <input type="text" v-model="newData.beginPayable" />
            </div>
          </div>
          <div v-show="newData.companyRAndPStatus" class="input_line p_width">
            <div>
              <p>累计应收款</p>
              <input type="text" v-model="newData.totalReceivable" />
            </div>
            <div>
              <p>累计应付款</p>
              <input type="text" v-model="newData.totalPayable" />
            </div>
          </div>
          <div v-show="newData.companyRAndPStatus" class="input_line p_width">
            <div>
              <p>应收信用额</p>
              <input type="text" v-model="newData.receivableQuota" />
            </div>
            <div>
              <p>应付信用额</p>
              <input type="text" v-model="newData.payableQuota" />
            </div>
          </div>
          <div v-show="newData.companyRAndPStatus" class="input_line p_width">
            <div>
              <p>收款期限(天)</p>
              <input type="text" v-model="newData.receivableTime" />
            </div>
            <div>
              <p>付款期限(天)</p>
              <input type="text" v-model="newData.payableTime" />
            </div>
          </div>

          <div class="input_textarea">
            <p>备注</p>
            <textarea rows="5" cols="5" v-model="newData.companyRemark"></textarea>
          </div>
        </div>
        <div class="dialog_bottom">
          <el-button type="primary" @click.stop="onAddSubmit">保存</el-button>
          <el-button @click.stop="dialogVisible = false">取消</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 选择类别 -->
    <DialogTree ref="treeRef" :toTreeData="toTreeData" @onSelectTree="onSelectTree"></DialogTree>
    <!-- 成功弹框 -->
    <PopupSuccess ref="successRef" :tipsData="tipsData"></PopupSuccess>
  </div>
</template>

<script>
import { regionData, CodeToText } from 'element-china-area-data'
import DialogTree from './dialogTree'
import PopupSuccess from './popupSuccess'
import { putAction } from '@/api/manage'
export default {
  components: {
    DialogTree,
    PopupSuccess,
  },
  props: {
    newData: {
      type: Object,
      default: () => ({
        companyName: '',
        companyUserName: '',
        xghCompanyNatureId: '',
        xghCompanyNature: '',
        companyPhone: '',
        companyPinyinCode: '',
        companyLandline: '',
        xghCompanyTypeId: '', //单位类别
        companyTypeName: '',
        business: '',
        provinceName: '', //省
        provinceCode: '',
        cityName: '', //市
        cityCode: '',
        countyName: '', //区县
        countyCode: '',
        address: '',
        setPrice: '',
        setDiscountPrice: '',
        companyInvoiceStatus: true, //开票信息
        depositBank: '',
        bankName: '',
        bakCard: '',
        taxNumber: '',
        invoiceAddress: '',
        bankPhone: '',
        companyRAndPStatus: true, //应收应付
        beginReceivable: 0,
        beginPayable: 0,
        totalReceivable: 0,
        totalPayable: 0,
        receivableQuota: '',
        payableQuota: '',
        receivableTime: '',
        payableTime: '',
        companyRemark: '',
        companyNumber: '', //单位编码
      }),
    },
  },
  updated () {
    this.updateCount = this.updateCount + 1
  },
  data () {
    return {
      // 传递给弹框的数据
      tipsData: {
        tipsState: '',
        tipsWords: '',
        tipsMark: '',
      },
      // 传递给选择类别的数据
      toTreeData: {
        module: {
          name: 'companyTypeName', //类别名称字段
          code: 'companyCode', //类别编码字段
          pyCode: 'companyPinyinCode', //类别拼音码字段
          indexCode: 'companyIndexCode', //类别排序码字段
        },
        getTreeUrl: '/xgh/companyType/treeList', //获取类别接口
        deleteTreeUrl: '/xgh/companyType/deleteById', //删除类别接口
        addTreeUrl: '/xgh/companyType/add', //新增类别接口
        editTreeUrl: '/xgh/companyType/edit', //修改类别接口
      },
      updateCount: 0, //判断用户是否更新当前数据
      ssqOptions: regionData, //省市区选项
      selectedOptions: [], //省市区
      // 单位性质
      CompanyNatureOptions: [
        {
          value: '1',
          label: '供应商',
        },
        {
          value: '2',
          label: '客户',
        },
        {
          value: '3',
          label: '供应商即客户',
        },
      ],
      // 单位类别
      dwlbOptions: [],
      // 预设售价
      PresetPriceOptions: [
        {
          value: '零售价',
          label: '零售价',
        },
        {
          value: '预设售价一',
          label: '预设售价一',
        },
        {
          value: '预设售价二',
          label: '预设售价二',
        },
        {
          value: '无',
          label: '无',
        },
      ],
      // 预设折扣
      PresetDiscountOptions: [
        {
          value: '固定折扣',
          label: '固定折扣',
        },
        {
          value: '预设折扣一',
          label: '预设折扣一',
        },
        {
          value: '预设折扣二',
          label: '预设折扣二',
        },
        {
          value: '无',
          label: '无',
        },
      ],
      dialogVisible: false,
    }
  },
  methods: {
    // 选择单位性质
    onCompanyNature (val) {
      this.newData.xghCompanyNatureId = val
    },
    // 打开选择类别弹出框
    onShowTree () {
      this.$refs.treeRef.getTreeData()
      this.$refs.treeRef.dialogVisible = true
    },
    // 选择当前类别
    onSelectTree (val) {
      this.newData.companyTypeName = val.companyTypeName
      this.newData.xghCompanyTypeId = val.id
      this.$forceUpdate()
    },
    // 获取省市区数据
    addressChange (value) {
      this.newData.provinceName = CodeToText[value[0]] //省
      this.newData.provinceCode = value[0]
      this.newData.cityName = CodeToText[value[1]] //市
      this.newData.cityCode = value[1]
      this.newData.countyName = CodeToText[value[2]] //区县
      this.newData.countyCode = value[2]
    },
    // 转换拼音码
    getNameToPinying (event) {
      this.newData.companyPinyinCode = this.$pinyin.getJp(event.target.value)
    },
    // 保存前的校验
    validateParams () {
      if (!this.newData.companyName.trim()) {
        this.$elMessage.error('单位名称不能为空')
        return false
      }
      if (!this.newData.companyNumber.trim()) {
        this.$elMessage.error('单位编码不能为空')
        return false
      }
      return true
    },
    onAddSubmit () {
      if (this.updateCount > 1) {
        // 校验名称和编码是否有值
        if (!this.validateParams()) {
          return
        }
        //更新次数大于1 说明用户修改过当前页数据
        this.newData.companyInvoiceStatus = this.newData.companyInvoiceStatus ? 0 : 1
        this.newData.companyRAndPStatus = this.newData.companyRAndPStatus ? 0 : 1
        this.newData.status = this.newData.status ? 0 : 1
        let data = {
          ...this.newData,
        }
        this.dialogVisible = !this.dialogVisible
        // 调接口添加供应商
        putAction('/xgh/company/edit', data).then((res) => {
          if (res.code === 200) {
            // 添加成功后，将成功后的商品信息返回到列表页面
            this.tipsData.tipsWords = res.message
            this.$refs.successRef.dialogVisible = true
            this.$emit('onAddSubmit', res)
          } else {
            this.$elMessage({
              showClose: true,
              message: res.message,
              type: 'error',
              iconClass: 'el-icon-warning-outline',
              duration: 3000,
            })
          }
        })
      } else {
        this.$elMessage({
          showClose: true,
          message: '请输入往来单位信息！',
          type: 'error',
          iconClass: 'el-icon-warning-outline',
          duration: 3000,
        })
      }
    },
    // 关闭事件发射
    dialogVisibleClose () {
      this.$emit('dialogVisibleClose')
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  .el-dialog__body {
    padding: 0;
  }
  .el-dialog__header {
    width: 100%;
    height: 0.937559rem;
    box-sizing: border-box;
    padding: 0 0.375023rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;
    span {
      font-size: 0.300019rem;
      color: #666;
      font-weight: 600;
    }
    .el-dialog__headerbtn {
      position: static;
      .el-icon-close {
        color: #999;
        font-size: 0.48753rem;
      }
    }
  }
}
.dialog_div_new {
  width: 100%;
  height: 60vh;
  overflow: auto;
  background: #fff;
  .dialog_middle {
    height: calc(100% - 1.12507rem);
    margin-top: 0;
    padding-bottom: 0;
    box-sizing: border-box;
    padding: 0 0.375023rem;
    overflow: auto;
    div {
      p {
        margin: 0;
        font-size: 0.225014rem;
        font-family: PingFang SC;
        font-weight: 400;
        color: #666666;
      }
      input {
        flex: 1;
        height: 0.562535rem;
        background: #ffffff;
        border: 1px solid #e3e3e3;
        border-radius: 0.075005rem;
        font-size: 0.225014rem;
        color: #666;
        box-sizing: border-box;
        padding: 0 0.187512rem;
        &:focus {
          outline: none;
          border-color: #1890ff;
        }
      }
      ::v-deep .el-select {
        margin-left: 0.562535rem;
        flex: 1;
        .el-input {
          .el-input__inner {
            height: 0.562535rem;
            background: #ffffff;
            border: 1px solid #e3e3e3;
            border-radius: 0.075005rem;
            font-size: 0.225014rem;
            color: #666;
            box-sizing: border-box;
            padding: 0 0.187512rem;
          }
          .el-input__suffix {
            height: 0.562535rem;
            display: flex;
            align-items: center;
          }
        }
      }
    }
    .input_switch {
      display: flex;
      height: 0.562535rem;
      margin-top: 0.281268rem;
      div {
        flex: 1;
        display: flex;
        align-items: center;
        margin-right: 0.375023rem;
        p {
          width: 0.937559rem;
          text-align: justify;
          text-justify: distribute-all-lines; /*ie6-8*/
          text-align-last: justify; /* ie9*/
          -moz-text-align-last: justify; /*ff*/
          -webkit-text-align-last: justify; /*chrome 20+*/
        }
        ::v-deep .el-switch {
          margin-left: 0.562535rem;
          flex: 0;
          .el-switch__core {
            width: 0.562535rem;
            height: 0.31877rem;
          }
          .el-switch__core:after {
            width: 0.281268rem;
            height: 0.281268rem;
            top: 0;
          }
          .is-checked .el-switch__core::after {
            margin-left: 0.281268rem !important;
          }
        }
        ::v-deep .el-switch.is-checked .el-switch__core::after {
          left: 100%;
          margin-left: -0.281268rem;
        }
        span {
          color: #fd6c21;
          font-size: 0.225014rem;
          margin-left: 0.562535rem;
        }
      }
    }
    .input_textarea {
      display: flex;
      align-items: flex-start;
      margin-top: 0.281268rem;
      p {
        width: 0.937559rem;
        text-align: justify;
        text-justify: distribute-all-lines; /*ie6-8*/
        text-align-last: justify; /* ie9*/
        -moz-text-align-last: justify; /*ff*/
        -webkit-text-align-last: justify; /*chrome 20+*/
      }
      textarea {
        margin-left: 0.562535rem;
        flex: 1;
        background: #ffffff;
        border: 1px solid #e3e3e3;
        border-radius: 0.075005rem;
        font-size: 0.225014rem;
        color: #666;
        box-sizing: border-box;
        padding: 0.187512rem;
        &:focus {
          outline: none;
          border-color: #1890ff;
        }
      }
    }
    .input_line {
      display: flex;
      height: 0.562535rem;
      margin-top: 0.281268rem;
      div {
        flex: 1;
        display: flex;
        align-items: center;
        &:first-child {
          margin-right: 0.187512rem;
        }
        &:last-child {
          margin-left: 0.187512rem;
        }
        ::v-deep .el-select {
          margin-left: 0.562535rem;
        }
      }
      > div {
        > p:first-child {
          display: block;
          width: 1.312582rem;
          width: 0.937559rem;
          text-align: justify;
          text-justify: distribute-all-lines; /*ie6-8*/
          text-align-last: justify; /* ie9*/
          -moz-text-align-last: justify; /*ff*/
          -webkit-text-align-last: justify; /*chrome 20+*/
        }
        input {
          margin-left: 0.562535rem;
        }
      }
      .line_dialog {
        width: 100%;
        .dialog_input {
          flex: 1;
          position: relative;
          display: flex;
          align-items: center;
          justify-content: flex-end;
          ::v-deep .el-select {
            margin-right: 0;
            .el-select-dropdown__item {
              p {
                margin: 0;
              }
            }
            i {
              display: none;
            }
          }
          i {
            position: absolute;
            margin-right: 0.187512rem;
            color: #bfbfbf;
            font-size: 0.281268rem;
            cursor: pointer;
          }
        }
      }
      .line_areaAddress {
        div {
          height: 0.562535rem;
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-left: 0;
          .address_china {
            flex: 0;
            width: 0.937559rem;
            height: 0.562535rem;
            text-align: center;
          }
          ::v-deep .el-cascader {
            flex: 1;
            margin-left: 0.187512rem;
            height: 0.562535rem;
            .el-input {
              display: flex;
              align-items: center;
              .el-input__inner {
                height: 0.562535rem;
                font-size: 0.225014rem;
                color: #666;
              }
              .el-input__suffix {
                height: 0.562535rem;
                display: flex;
                align-items: center;
              }
            }
          }
        }
      }
      .line_detailedAddress {
        ::v-deep .el-button {
          width: 0.750047rem;
          height: 0.562535rem;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 0.225014rem;
          margin-left: 0.093756rem;
          background: #1890ff;
        }
      }
    }
    .p_width {
      > div {
        > p:first-child {
          display: block;
          width: 1.312582rem;
          text-align: justify;
          text-justify: distribute-all-lines; /*ie6-8*/
          text-align-last: justify; /* ie9*/
          -moz-text-align-last: justify; /*ff*/
          -webkit-text-align-last: justify; /*chrome 20+*/
        }
        input {
          margin-left: 0.187512rem;
        }
      }
      div {
        .el-switch {
          margin-left: 0.187512rem;
        }
      }
    }
  }
  .dialog_bottom {
    width: 100%;
    height: 1.12507rem;
    box-sizing: border-box;
    padding: 0 0.187512rem;
    display: flex;
    align-items: center;
    justify-content: center;
    ::v-deep .el-button {
      width: 1.12507rem;
      height: 0.562535rem;
      text-align: center;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 0.225014rem;
      border-radius: 0.093756rem;
      background: #fff;
      border: 1px solid #dddddd;
      color: #666666;
    }
    ::v-deep .el-button--primary {
      background: #1890ff;
      border-color: #1890ff;
      color: #fff;
      margin-right: 0.112507rem;
    }
    ::v-deep .el-button--default {
      margin-left: 0.112507rem;
    }
  }
}
</style>